package org.zuel.boot.demo.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.zuel.boot.demo.entity.StudentInfo;
import org.zuel.boot.demo.entity.SysUser;
import org.zuel.boot.demo.mapper.StudentInfoMapper;
import org.zuel.boot.demo.web.*;
import org.zuel.boot.demo.exception.ParameterException;
import org.zuel.boot.demo.entity.Company;
import org.zuel.boot.demo.mapper.CompanyMapper;
import org.zuel.boot.demo.service.ICompanyService;
import org.springframework.stereotype.Service;

import java.util.*;


/**
 *  服务实现类
 *
 * @author ikun
 */
@Service
public class CompanyServiceImpl extends ServiceImpl<CompanyMapper, Company> implements ICompanyService {
    @Autowired
    SysDictServiceImpl sysDictService;
    private final StudentInfoMapper studentInfoMapper;

    public CompanyServiceImpl(StudentInfoMapper studentInfoMapper) {
        this.studentInfoMapper = studentInfoMapper;
    }

    @Override
    public IPage<Company> list(PageParam<Company> pageParam) {
        QueryWrapper<Company> queryWrapper = new QueryWrapper<>();
        if (pageParam.getPageData() != null) {
            if (pageParam.getPageData().get("name") != null) {
                queryWrapper.like("name", pageParam.getPageData().get("name"));
            }
            if (pageParam.getPageData().get("nature") != null) {
                queryWrapper.like("nature", pageParam.getPageData().get("nature"));
            }
            if (pageParam.getPageData().get("industry") != null) {
                queryWrapper.like("industry", pageParam.getPageData().get("industry"));
            }
            if (pageParam.getPageData().get("province") != null) {
                queryWrapper.like("province", pageParam.getPageData().get("province"));
            }
            if (pageParam.getPageData().get("city") != null) {
                queryWrapper.like("city", pageParam.getPageData().get("city"));
            }
        }
        Page<Company> page = new Page<>(pageParam.getCurrent(), pageParam.getSize());
        return this.page(page, queryWrapper);
    }

    @Override
    public JsonResult read(Integer id) {
        if (id == null) {
            throw new ParameterException("参数对象为空");
        }

        Company company = baseMapper.selectById(id);
        return JsonResult.ok().setData(company);
    }
    @Override
    public JsonResult readRandom(SysUser user) {
        // 创建一个随机数生成器
        Random random = new Random();
        List<Integer> randomIds = new ArrayList<>();

        // 生成5个1到16000之间的随机数
        while (randomIds.size() < 5) {
            int randomId = random.nextInt(16000) + 1;
            if (!randomIds.contains(randomId)) {
                randomIds.add(randomId);
            }
        }

        QueryWrapper<StudentInfo> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.in("user_id", user.getUserId());
        StudentInfo studentInfo = studentInfoMapper.selectOne(queryWrapper1);

        // 查询这些随机ID对应的公司
        QueryWrapper<Company> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.in("id", randomIds);
        queryWrapper2.notIn("website", "");
        if(!Objects.equals(studentInfo.getYxdwxz(), "0")){
            queryWrapper2.in("nature", studentInfo.getYxdwxz());
        }
        queryWrapper2.isNotNull("website"); // 添加非空条件
        List<Company> companies = baseMapper.selectList(queryWrapper2);

        return JsonResult.ok().setData(companies);
    }

    @Override
    public JsonResult add(Company company) {
        if (company ==null){
            throw new ParameterException("参数对象为空");
        }

        baseMapper.insert(company);
        return JsonResult.ok("添加成功");
    }

    @Override
    public JsonResult update(Company company) {
        if (company == null) {
            throw new ParameterException("参数对象为空");
        }

        if (company.getId() == null) {
            throw new ParameterException("参数对象id为空");
        }

        baseMapper.updateById(company);
        return JsonResult.ok("修改成功");
    }

    @Override
    public JsonResult delete(Integer[] ids) {
        if (ids == null) {
            throw new ParameterException("参数对象为空");
        }

        if (ids != null || ids.length > 0) {
            baseMapper.deleteBatchIds(Arrays.asList(ids));
        }
        return JsonResult.ok("删除成功");
    }

}